﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Waker.Common;
using Waker.Entitys;

namespace Waker.SystemSetting
{
    public partial class FormBaseDataEdit : Waker.Base.FormBasePop
    {
        DataRow _DrSource;
        bool _IsAdd;
        public FormBaseDataEdit(DataRow dr, bool isAdd)
        {
            InitializeComponent();
            _DrSource = dr;
            _IsAdd = isAdd;
        }
        private void FormBaseDataEdit_Load(object sender, EventArgs e)
        {
            List<string> listDataType = new List<string>();
            //listDataType.Add(Enums.BaseDataType.Education);
            //listDataType.Add(Enums.BaseDataType.Nation);
            listDataType.Add(Enums.BaseDataType.Job);
            listDataType.Add(Enums.BaseDataType.ConstructionType);
            listDataType.Add(Enums.BaseDataType.WorkType);
            listDataType.ForEach(t =>
            {
                cmbDataType.Properties.Items.Add(t);
            });
            if (_IsAdd)
            {
                if (_DrSource != null)
                    this.cmbDataType.SelectedItem = _DrSource["DataType"];
            }
            else
            {
                ControlUtil.BindData(this.Controls, _DrSource);
            }
        }
        protected override void FormSave()
        {
            if (string.IsNullOrEmpty(cmbDataType.Text))
            {
                base.errorPop.SetError(cmbDataType, "类型不能为空");
                return;
            }
            if (string.IsNullOrEmpty(txtDataName.Text))
            {
                base.errorPop.SetError(txtDataName, "名称不能为空");
                return;
            }
            var bal=new BAL.BAL_SystemSetting();
            if (bal.CheckBaseData(cmbDataType.Text, txtDataName.Text))
            {
                base.errorPop.SetError(txtDataName, "名称已经存在,不能重复");
                return;
            }
            var isOk = bal.SaveBaseData(
                    _IsAdd ? 0 : Convert.ToInt32(_DrSource["ID"])
                    , cmbDataType.Text
                    , txtDataName.Text
                    , txtRemark.Text
                    );
            if (isOk)
            {
                UI.ShowInfo("保存成功");
                this.DialogResult = DialogResult.OK;
            }
            else
            {
                UI.ShowError("保存失败");
            }
        }


    }
}
